package com.example.ibsp_demo.api;

/**
 * 统一API响应格式
 * @param <T> 数据类型
 */
public class ApiMessage<T> {
    private int code;       // 状态码
    private String message; // 消息
    private T data;         // 数据
    private long timestamp; // 时间戳

    public ApiMessage() {
        this.timestamp = System.currentTimeMillis();
    }

    /**
     * 成功响应（无数据）
     */
    public static <T> ApiMessage<T> success() {
        ApiMessage<T> result = new ApiMessage<>();
        result.setCode(200);
        result.setMessage("success");
        return result;
    }

    /**
     * 成功响应（带数据）
     */
    public static <T> ApiMessage<T> success(T data) {
        ApiMessage<T> result = new ApiMessage<>();
        result.setCode(200);
        result.setMessage("success");
        result.setData(data);
        return result;
    }

    /**
     * 失败响应
     */
    public static <T> ApiMessage<T> fail(int code, String message) {
        ApiMessage<T> result = new ApiMessage<>();
        result.setCode(code);
        result.setMessage(message);
        return result;
    }

    // Getters and Setters
    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public long getTimestamp() {
        return timestamp;
    }

    public void setTimestamp(long timestamp) {
        this.timestamp = timestamp;
    }
}